package com.gz.mylife.wxapi;

import com.gz.mylife.Define;
import com.gz.mylife.R;
import com.gz.mylife.app.BaseActivity;
import com.gz.mylife.util.ObserverWorker;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.zl.smartmall.library.util.CrashHandler;

import android.os.Bundle;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class WXPayEntryActivity extends BaseActivity implements IWXAPIEventHandler{

    private static final String TAG = WXPayEntryActivity.class.getSimpleName();
    private IWXAPI api;
    
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_wxpay_entry);

    	api = WXAPIFactory.createWXAPI(this, Define.APP_ID_WECHAT);
        api.handleIntent(getIntent(), this);
        
//        mActionBar.setTitle("支付结果");
		setActionbarTitle("支付结果", false);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.wxpay_entry, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case android.R.id.home:
//			Intent intent = new Intent(getApplicationContext(), MyOrders.class);
//			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//			intent.putExtra("tab_index", 3);
//			startActivity(intent);
			ObserverWorker.getInstance().dispatchMessage(Define.OBSERVER_TYPE_REFRESH_ORDER_DETAIL);
			finish();		
			return true;

		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
//		super.onBackPressed();
//		Intent intent = new Intent(getApplicationContext(), Plaza.class);
//		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//		intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
//		intent.putExtra("tab_index", 3);
//		startActivity(intent);
		ObserverWorker.getInstance().dispatchMessage(Define.OBSERVER_TYPE_REFRESH_ORDER_DETAIL);
		finish();
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onResp(BaseResp resp) {
		// TODO Auto-generated method stub
		CrashHandler.d(TAG, "onPayFinish, errCode = " + resp.errCode);
		TextView tvResult1 = (TextView) findViewById(R.id.tv_1);
		TextView tvResult2 = (TextView) findViewById(R.id.tv_2);
		TextView tvResult3 = (TextView) findViewById(R.id.tv_3);
		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			String result1 = "";
			String result2 = "";
			String result3 = "";
			switch (resp.errCode) {
			case BaseResp.ErrCode.ERR_OK://成功
//				mResult.setText("亲, 您已成功支付, 请注意查收商品");
				result1 = "交易成功!";
				result2 = " 我们将立即为您发货";
				result3 = "返回订单列表查看发货进度";
				break;

			case BaseResp.ErrCode.ERR_COMM://失败
//				mResult.setText("支付未成功, 请重新支付");
				result1 = "交易失败!";
				result3 = "返回订单列表查看详情";
				break;
				
			case BaseResp.ErrCode.ERR_USER_CANCEL://取消支付
//				mResult.setText("亲, 您已取消支付");
				result1 = "交易取消!";
				result3 = "返回订单列表查看详情";
				break;
			}
			
			tvResult1.setText(result1);
			tvResult2.setText(result2);
			tvResult3.setText(result3);
			
			CrashHandler.d(TAG, "支付信息" + resp.errStr);
			
//			AlertDialog.Builder builder = new AlertDialog.Builder(this);
//			builder.setTitle("提示");
////			builder.setMessage(getString(R.string.hint_wechat_pay_result, String.valueOf(resp.errCode)));
//			builder.setMessage(result);
//			builder.show();
		}
	}

	
}
